
const app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    userInfo: {}

  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let self = this;
  },
  //用户授权
  getUserInfo: function (e) {
    let self = this;
    console.log(e)
    app.globalData.userInfo = e.detail.userInfo
    this.setData({
      userInfo: e.detail.userInfo
    })
    self.getOpenId()
  },
  //获取用户openid
  getOpenId: function () {
    let self = this
    wx.login({
      timeout: 10000,
      success: (result) => {
        if (result.code) {
          wx.request({
            url: `${app.globalData.host}/api/miniapps/getUserOpenId`,
            data: { code: result.code },
            success: res => {
              self.data.userInfo.openId = res.data.data.openid
              self.addMemberInfo(self.data.userInfo)
            }
          })
        }
      },
      fail: () => { },
      complete: () => { }
    });
  },
  //保存会员信息
  addMemberInfo: function (member) {
    let self = this
    wx.request({
      url: `${app.globalData.host}/api/miniapps/addMemberInfo`,
      data: {
        AvatarUrl: member.avatarUrl,
        Country: member.country,
        Province: member.province,
        City: member.city,
        Gender: member.gender,
        Language: member.language,
        NickName: member.nickName,
        OpenId: self.data.userInfo.openId,
        userId: self.data.userInfo.openId
      },
      method: "POST",
      success: res => {
        wx.setStorageSync("userId", self.data.userInfo.openId);
        //跳转到首页
        wx.switchTab({
          url: '/pages/home/index'
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    let that = this;
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },


  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})